package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import dao.DomainDAORemote;
import dto.DomainDTO;

@ManagedBean
@SessionScoped
public class DomainController implements Serializable {
	private static final long serialVersionUID = 1L;

	@EJB
	DomainDAORemote domainDAO;

	@ManagedProperty(value = "#{domainDTO}")
	DomainDTO domainDTO;

	@ManagedProperty(value = "#{categoryController}")
	CategoryController categoryController;

	@ManagedProperty(value = "#{menuController}")
	MenuController menuController;

	private List<DomainDTO> listOfDomains;
	private List<DomainDTO> listOfD;
	private List<String> checkbox = new ArrayList<String>();
	private String messageClass;

	@PostConstruct
	public void createList() {
		listOfDomains = domainDAO.findAllDomains();
		listOfD = listOfDomains;
		
	}

	public MenuController getMenuController() {
		return menuController;
	}

	public void setMenuController(MenuController menuController) {
		this.menuController = menuController;
	}

	public DomainDTO getDomainDTO() {
		return domainDTO;
	}

	public CategoryController getCategoryController() {
		return categoryController;
	}

	public void setCategoryController(CategoryController categoryController) {
		this.categoryController = categoryController;
	}

	public void setDomainDTO(DomainDTO domainDTO) {
		this.domainDTO = domainDTO;
	}

	public void getAllDomains(ActionEvent actionEvent) {
		listOfDomains = domainDAO.findAllDomains();
	}

	public void addDomain(ActionEvent event) {
		try {
			if (domainDTO.getName() == "" || domainDTO.getName() == null) {
				FacesContext context = FacesContext.getCurrentInstance();
				context.addMessage(event.getComponent().getClientId(), new FacesMessage("Please, type a domain!"));
				messageClass = "errorMessage";
			} else if (domainDAO.getIdByDomainName(domainDTO.getName()) != 0) {
				FacesContext context = FacesContext.getCurrentInstance();
				String clientId = event.getComponent().getClientId(context);
				context.addMessage(clientId, new FacesMessage("The domain allready exists!"));
				messageClass = "errorMessage";
			} else {
				domainDAO.addDomain(domainDTO);
				listOfDomains = domainDAO.findAllDomains();
				listOfD.clear();
				for (DomainDTO dDTO : listOfDomains) {
					listOfD.add(dDTO);
				}
			}

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		domainDTO.setName("");
	}

	public void removeDomain() {
		try {
			domainDAO.removeDomain(domainDTO);
			listOfDomains = domainDAO.findAllDomains();
			categoryController.setCurrentDomainId(-1);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public void setListOfDomains(List<DomainDTO> listOfDomains) {
		this.listOfDomains = listOfDomains;
	}

	public List<DomainDTO> getListOfDomains() {
		return listOfDomains;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox.addAll(checkbox);
	}

	public List<String> getCheckbox() {
		return checkbox;
	}

	public void deleteDomains(ActionEvent event) {
		if (checkbox.size() != 0) {
			for (String s : checkbox) {
				DomainDTO d = new DomainDTO();
				d.setId(Integer.parseInt(s));
				try {
					if (domainDAO.domainHasCategories(d) == true) {
						String dName = "";
						for (DomainDTO dd : listOfDomains)
							if (dd.getId() == d.getId())
								dName = dd.getName();

						FacesContext context = FacesContext.getCurrentInstance();
						String clientId = event.getComponent().getClientId(context);
						context.addMessage(clientId, new FacesMessage("Cannot delete domain " + dName
								+ ". Domain has categories!"));
						messageClass = "errorMessage";
					} else {
						domainDAO.removeDomain(d);
						categoryController.setCurrentDomainId(-1);
					}
				} catch (Exception e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
			checkbox.clear();
			listOfDomains = domainDAO.findAllDomains();
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			String clientId = event.getComponent().getClientId(context);
			context.addMessage(clientId, new FacesMessage("Please select at least one domain!"));
			messageClass = "errorMessage";
		}

	}

	public void changeSelected(ValueChangeEvent event) {
		categoryController.setCurrentDomainId(Integer.parseInt((String) event.getNewValue()));
	}
	
	public List<DomainDTO> getListOfD() {
		if (categoryController.getCurrentDomainId() == -1)
			if (listOfD.size() > 0) {
				categoryController.setCurrentDomainId(listOfD.get(0).getId());
			}
		return listOfD;
	}

	public void setListOfD(List<DomainDTO> listOfD) {
		this.listOfD = listOfD;
	}

	/**
	 * @param messageClass the messageClass to set
	 */
	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	/**
	 * @return the messageClass
	 */
	public String getMessageClass() {
		return messageClass;
	}

}
